package com.coderfans.htmlparser;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import us.codecraft.xsoup.Xsoup;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.RecursiveTask;

/**
 * Created by ${fanchunshuai} on 2017-7-11.
 *
 * @version: V1.0
 * @Desc:
 * @Copyright (c) 2017 58到家-版权所有
 */
public class HtmlProcessor extends RecursiveTask<List<String>> {
    private String path;
    private String extension;
    private  Map<String,String> map;
    private String insertSql = "INSERT INTO csdn_blog (user_id,category_id,title ,tag_list,is_owner,blog_url,cai_num,zan_num,comm_num,read_num,sim_links,create_time)";
    public HtmlProcessor(String path, String extension,Map<String,String> map) {
        this.path = path;
        this.extension = extension;
        this.map = map;
    }

    @Override
    protected List<String> compute() {
        //存储文件夹中的名称
        List<String> list = new ArrayList<String>();

        //存储子任务，这些子任务将处理文件夹中的子文件夹
        List<HtmlProcessor> tasks = new ArrayList<HtmlProcessor>();

        File file = new File(path);

        File content [] = file.listFiles();
        File writeSql = new File("E:\\spiderFile\\csdnBlogSql.log");

        if(content != null){
            for (int i = 0; i < content.length; i++) {
                if(content[i].isDirectory()){
                    HtmlProcessor task = new HtmlProcessor(content[i].getAbsolutePath(), extension,map);
                    //如果是子文件夹，使用该方法采用异步的方式执行
                    task.fork();
                    tasks.add(task);
                }else{
                    if(checkFile(content[i].getName())){
                        String userName = content[i].getParentFile().getName();
                        Document document = null;
                        try {
                            document = Jsoup.parse(content[i], "UTF-8");
                            //提取博客标题
                            String  titlePath = "//div[@id='article_details']//div[@class='article_title']/h1/span/a/text()";
                            List<String> titleList = Xsoup.compile(titlePath).evaluate(document).list();
                           // System.out.println(titleList.toString());

                            //是否原创
                            String ownerPath =  "//div[@id='article_details']//div[@class='article_title']/span/@class";
                            List<String> ownerList = Xsoup.compile(ownerPath).evaluate(document).list();
                            //System.out.println(ownerList.toString());

                            //博客标签
                            String tagPath =  "//div[@class='article_manage']//div[@class='article_l']/span//a/text()";
                            List<String> tagList = Xsoup.compile(tagPath).evaluate(document).list();
                            //System.out.println(tagList.toString());

                            //博客分类
                            String typePath = "//div[@class='category clearfix']//div[@class='category_r']/label/span/text()";
                            List<String> typeList = Xsoup.compile(typePath).evaluate(document).list();
                            //System.out.println(typeList.toString());

                            //发布时间,当前阅读量，当前评论数量
                            String pubTimePath = "//div[@id='article_details']/div[@class='article_manage clearfix']/div[@class='article_r']/span/text()";
                            List<String> pubTimeList = Xsoup.compile(pubTimePath).evaluate(document).list();

                            String createTime = "";
                            int readNum = 0;
                            int commNum = 0;
                            if(!pubTimeList.isEmpty() && pubTimeList.size() > 0){
                                createTime = pubTimeList.get(0);
                                readNum = Integer.parseInt(pubTimeList.get(1).replace("人阅读",""));
                                commNum = Integer.parseInt(pubTimeList.get(2).trim().replace("(","").replace(")","").toString());
                            }

                            //赞的数量
                            String zanNumPath = "//div[@id='article_details']/div[@id='digg']/dl[@id='btnDigg']/dd/text()";
                            List<String> zanNumList = Xsoup.compile(zanNumPath).evaluate(document).list();
                            //System.out.println(zanNumList);

                            //踩的数量
                            String caiNumPath = "//div[@id='article_details']/div[@id='digg']/dl[@id='btnBury']/dd/text()";
                            List<String> caiNumList = Xsoup.compile(caiNumPath).evaluate(document).list();
                            //System.out.println(caiNumList);

                            //相关性文章
                            String similarPath = "//div[@class='similar_article']/div[@class='similar_c']//a/@href";
                            List<String> simBlogLinkList = Xsoup.compile(similarPath).evaluate(document).list();
                            //System.out.println(simBlogLinkList);

                            //文章评论?????
                            //String commPath =" //div[@class='comment_class']/div[@id='comment_list']";
                            //List<String> commList = Xsoup.compile(commPath).evaluate(document).list();
                            //System.out.println(commList);
                            StringBuffer buffer = new StringBuffer(insertSql);
                            buffer.append(" VALUES(");
                            //user_id
                            buffer.append(map.get(userName));
                            buffer.append(",");
                            //category_id
                            buffer.append("'"+typeList.toString()+"'");
                            buffer.append(",");

                            String title = titleList.isEmpty() ? "" : titleList.get(0);

                            //title
                            buffer.append("'"+title.replace("'","")+"'");
                            buffer.append(",");
                            //tag_list
                            buffer.append("'"+tagList.toString()+"'");
                            buffer.append(",");
                            //是否原创
                            String ownerTag = ownerList.isEmpty() ? "":ownerList.get(0).toString();
                            if(Objects.equals("ico ico_type_Original",ownerTag)){
                                buffer.append(1);
                            }else{
                                buffer.append(0);
                            }
                            buffer.append(",");

                            //博客地址
                            buffer.append("'"+userName+"/"+content[i].getName().replace("'","")+"'");
                            buffer.append(",");
                            int caiNum = caiNumList.isEmpty() ? 0 : Integer.parseInt(caiNumList.get(0).trim());

                            //cai_num
                            buffer.append(caiNum);
                            buffer.append(",");

                            int zanNum = zanNumList.isEmpty() ? 0 : Integer.parseInt(zanNumList.get(0).trim());
                            //zan_num
                            buffer.append(zanNum);
                            buffer.append(",");

                            //comm_num
                            buffer.append(commNum);
                            buffer.append(",");


                            //read_num
                            buffer.append(readNum);
                            buffer.append(",");

                            //simple_links
                            buffer.append("'"+simBlogLinkList.toString()+"'");
                            buffer.append(",");

                            if(StringUtils.isBlank(createTime)){
                                //createTime
                                buffer.append("2017-07-25 00:00:00");
                            }else{
                                //createTime
                                buffer.append("'"+createTime+"'");
                            }


                            buffer.append(");");
                            Files.append(buffer.toString()+"\n",writeSql, Charsets.UTF_8); //追加文件


                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        list.add(content[i].getAbsolutePath());
                    }
                }
            }
        }
        if(tasks.size()> 50){
            System.out.printf("%S: %d tasks ran.\n",file.getAbsolutePath(),tasks.size());
        }
        addResultsFromTasks(list, tasks);
        return list;
    }
    //将通过这个任务而启动的子任务返回的结果增加到文件列表中，
    //参数：字符串列表list, FolderProcessor子任务列表tasks
    private void addResultsFromTasks(List<String> list,List<HtmlProcessor> tasks){
        for(HtmlProcessor  item: tasks){
            list.addAll(item.join());
        }
    }

    public boolean checkFile(String name){
        return name.endsWith(extension);
    }



}
